package com.nuo.backend.modules.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nuo.backend.modules.security.dao.SecRoleDao;
import com.nuo.backend.modules.security.entity.SecRoleEntity;
import com.nuo.backend.modules.security.entity.SecRoleMenuEntity;
import com.nuo.backend.modules.security.service.SecRoleMenuService;
import com.nuo.backend.modules.security.service.SecRoleService;
import com.nuo.backend.modules.shiro.entity.SysMenuEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuo.backend.common.utils.PageUtils;
import com.nuo.backend.common.utils.Query;

import com.nuo.backend.modules.security.dao.SecMenuDao;
import com.nuo.backend.modules.security.entity.SecMenuEntity;
import com.nuo.backend.modules.security.service.SecMenuService;


@Service("secMenuService")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SecMenuServiceImpl extends ServiceImpl<SecMenuDao, SecMenuEntity> implements SecMenuService {

    final SecRoleMenuService secRoleMenuService;
    final SecRoleDao secRoleDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SecMenuEntity> page = this.page(
                new Query<SecMenuEntity>().getPage(params),
                new QueryWrapper<SecMenuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SecMenuEntity> getSecMenuList() {
        List<SecMenuEntity> secMenuList = list();
        secMenuList.stream().forEach(i -> {
            List<Long> secRoleIdList = secRoleMenuService.list(new LambdaQueryWrapper<SecRoleMenuEntity>().eq(SecRoleMenuEntity::getMenuId, i.getMenuId())).stream().map(SecRoleMenuEntity::getRoleId).distinct().collect(Collectors.toList());
            i.setSecRoleList(secRoleDao.selectBatchIds(secRoleIdList));
        });
        return secMenuList;
    }

    @Override
    public List<SecMenuEntity> getUserMenuList(List<Long> secMenuIdList) {
        // 查询拥有的所有菜单
        List<SecMenuEntity> secMenuList = this.baseMapper.selectList(new LambdaQueryWrapper<SecMenuEntity>()
                .in(Objects.nonNull(secMenuIdList), SecMenuEntity::getMenuId, secMenuIdList)
                .in(SecMenuEntity::getType, 0, 1)
        );

        // 将id和菜单绑定
        Map<Long, SecMenuEntity> secMenuMap = secMenuList.stream().collect(Collectors.toMap(SecMenuEntity::getMenuId, Function.identity()));

        // 组装菜单的层级关系
        for (int i = 0; i < secMenuList.size();) {
            SecMenuEntity parentMenu = secMenuMap.get(secMenuList.get(i).getParentId());
            if (Objects.nonNull(parentMenu)) {
                parentMenu.getList().add(secMenuList.get(i));
                // 将这个菜单从当前节点移除
                secMenuList.remove(i);
            } else {
                i ++;
            }
        }

        return secMenuList;
    }

}